package com.zj.juc.study.interrupted;

import java.util.concurrent.TimeUnit;

/**
 * @author 自己的名字
 * @version 1.0.0
 * @ClassName InterrupteByThreadApiDemo
 * @Description 根据线程自带的api中断线程
 * @createTime 2024年12月26日 21:10
 */
public class InterruptedByThreadApiDemo {
    public static void main(String[] args) {
        Thread t1 =  new Thread(()->{
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("i have bean interrupted");
                    break;
                }
                System.out.println("i am running");
            }
        },"t1");

        t1.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(t1::interrupt,"t2").start();
    }
}
